/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package me.send.produce.data.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Pager;
import me.send.business.base.aliwantu.AliWanTu;
import me.send.business.base.oss.OSSObjectSample;
import me.send.data.service.bean.ReturnJsonBean;
import me.send.data.service.pojo.Advert;
import me.send.data.service.pojo.City;
import me.send.data.service.pojo.Manager;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.framework.util.HelperUtil;
import me.send.framework.util.LoggerUtils;
import me.send.produce.data.controller.common.CommonController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.media.Result;
import com.alibaba.media.client.MediaClient;

/**
 * @author maxmin
 *
 * @date 2015年9月23日 下午8:07:42
 */
@Controller
@RequestMapping(value = "/advert")
public class AdvertController extends CommonController {

	@RequestMapping(value = "/list")
	public ModelAndView toAdvertList(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("stage/advert/advert_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "advert/list"));
		modelAndView.addObject("module", "首页管理");
		return modelAndView;
	}

	@RequestMapping(value = "/list/data")
	@ResponseBody
	public DatatablesViewPage<Advert> advertList(HttpServletRequest request,
			Pager page, String cityName, String startTime, String endTime,
			String keyCode) {
		DatatablesViewPage<Advert> vp = new DatatablesViewPage<Advert>();
		Manager manager = getCurrentManager(request);
		if (manager != null) {
			String cn = manager.getCityName();
			if (cn != null && !"".equals(cn)) {
				List<Advert> advertList = advertService.getAdvertList(page,
						cityName, startTime, endTime, keyCode, cn);
				int advertCount = advertService.getAdvertCount(cityName,
						startTime, endTime, keyCode, cn);
				vp.setiTotalDisplayRecords(advertCount);
				vp.setiTotalRecords(advertCount);
				vp.setAaData(advertList);
			}
		}
		return vp;
	}

	@RequestMapping(value = "/new")
	public ModelAndView newAdvert(HttpServletRequest request) {
		Manager manager = getCurrentManager(request);
		List<City> cities = cityService.getCitiesList();
		ModelAndView modelAndView = new ModelAndView("stage/advert/advert_new");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "advert/list"));
		modelAndView.addObject("module", "首页管理");
		modelAndView.addObject("manager",manager);
		modelAndView.addObject("cities",cities);
		return modelAndView;
	}
	@RequestMapping(value = "/edit")
	public ModelAndView edit(HttpServletRequest request, int advertid) {
		Manager manager = getCurrentManager(request);
		List<City> cities = cityService.getCitiesList();
		ModelAndView modelAndView = new ModelAndView("stage/advert/advert_edit");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "advert/list"));
		modelAndView.addObject("module", "首页管理");
		modelAndView.addObject("manager",manager);
		modelAndView.addObject("cities",cities);
		modelAndView.addObject("advert", advertService.getAdvertById(advertid));
		return modelAndView;
	}

	@RequestMapping(value = "/add")
	@ResponseBody
	public ReturnJsonBean add(HttpServletRequest request,Advert advert) {
		ReturnJsonBean rb = new ReturnJsonBean();
		int result = advertService.addAdvert(advert);
		if (result > 0) {
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
		}
		return rb;
	}
	@RequestMapping(value = "/delete")
	@ResponseBody
	public ReturnJsonBean delete(HttpServletRequest request, int advertid) {
		ReturnJsonBean rb = new ReturnJsonBean();
		int result = advertService.deleteAdvertById(advertid);
		if (result > 0) {
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
		}
		return rb;
	}

	@RequestMapping(value = "/update")
	@ResponseBody
	public ReturnJsonBean update(HttpServletRequest request, Advert advert) {
		ReturnJsonBean rb = new ReturnJsonBean();
		if (advert != null) {
			int result = advertService.updateAdvert(advert);
			if (result>0) {
				rb.setCode(ReturnJsonBean.SUCCESS);
				rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			}
		}
		return rb;
	}

	@RequestMapping(value = "/upload/advertpic")
	@ResponseBody
	public ReturnJsonBean uploadAdvertPic(MultipartFile pic, int advertid) {
		ReturnJsonBean rb = new ReturnJsonBean();
		String originalFilename = pic.getOriginalFilename();
		String format = originalFilename.substring(originalFilename
				.lastIndexOf("."));
		String key = advertid + "_pic_" + System.currentTimeMillis() + "_"
				+ HelperUtil.uuid().substring(0, 3) + format;
		String resultPath = OSSObjectSample.OSSPutObject(key, pic,
				AliWanTu.ADVERT_DIR);
		if (resultPath != null && !"".equals(resultPath)) {
			Advert advert = advertService.getAdvertById(advertid);
			String picpath = advert.getPicpath();
			int index = picpath.lastIndexOf("/");
			String substring = picpath.substring(index + 1);
			int indexOf = substring.indexOf("?");
			String name = "";
			if (indexOf > -1) {
				name = substring.substring(0, indexOf);
			}else {
				name=substring;
			}
			int updateResult = advertService.updateAdvertPic(advertid,
					resultPath);
			if (updateResult > 0) {
				LoggerUtils.info(getClass().getName(), "更新成功");
				MediaClient client = AliWanTu.getMediaClient();
				String dir = "/"+AliWanTu.ADVERT_DIR;
				Result<Void> result = client.deleteFile(dir, name);
				LoggerUtils.info(getClass().getName(), "result:"+result);
				if (result.isSuccess()) {
					// 删除成功
					rb.setCode(ReturnJsonBean.SUCCESS);
					rb.setMessage(ReturnJsonBean.SUCCESS_STR);
					rb.setDatas(resultPath);
				}
			} else {
				LoggerUtils.info(getClass().getName(), "更新失败");
			}
		}
		return rb;
	}
	@RequestMapping(value = "/upload/advertpic/add")
	@ResponseBody
	public ReturnJsonBean addAdvertPic(HttpServletRequest request,MultipartFile pic) {
		ReturnJsonBean rb = new ReturnJsonBean();
		String originalFilename = pic.getOriginalFilename();
		String format = originalFilename.substring(originalFilename
				.lastIndexOf("."));
		Manager manager = getCurrentManager(request);
		String key = manager.getManagerId() + "_pic_" + System.currentTimeMillis() + "_"
				+ HelperUtil.uuid().substring(0, 3) + format;
		String resultPath = OSSObjectSample.OSSPutObject(key, pic,
				AliWanTu.ADVERT_DIR);
		if (resultPath!=null) {
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			rb.setDatas(resultPath);
		}
		return rb;
	}
}
